<template>
	<div>
		<ProTable ref="proTable" title="年份酒库存数据" :columns="columns" :request-api="getTableList" :init-param="initParam">
			<!-- Expand -->
			<template #expand="scope">
				{{ scope.row }}
			</template>

			<!-- 表格 功能 按钮 -->
			<template #otherButton>
				<el-button @click="onExport" v-auth="'data:stock-in-out:analysis:export'"> 导出 </el-button>
			</template>
		</ProTable>
	</div>
</template>

<script setup lang="tsx" name="dataWarehouse">
import { onMounted, ref, reactive, computed } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useRouter, useRoute } from 'vue-router';
import { NextLoading } from '/@/utils/loading';
import ProTable from '../../../../components/ProTable/index.vue';
import { ProTableInstance, ColumnProps } from '../../../../components/ProTable/interface';
import { fetchInventoryList, fetchInventoryExport } from '/@/api/data';
import { useDownload } from '/@/hooks/useDownload';

const handleParams = (params: any) => {
	let data = {
		...params,
	};

	return data;
};

// 保存并导出
const onExport = () => {
	ElMessageBox.confirm('确认导出数据?', '温馨提示', { type: 'warning' }).then(() =>
		useDownload(fetchInventoryExport, '年份酒库存数据', handleParams(proTable.value?.searchParam))
	);
};

// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref<ProTableInstance>();

// 如果表格需要初始化请求参数，直接定义传给 ProTable(之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({
	searchType: 'materialCode',
});

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getTableList"
const getTableList = (params: any) => {
	let data = {
		...params,
	};

	return fetchInventoryList(data);
};

const searchArr = [
	{
		label: '物料编码',
		value: 'materialCode',
	},
	{
		label: '物料名称',
		value: 'materialName',
	},
];

// 表格配置项
const columns: ColumnProps<any>[] = [
	{
		prop: 'keywords',
		label: '物料搜索',
		isShow: false,
		search: {
			span: 2,
			render: (scope: any) => {
				const slot = {
					prepend: () => (
						<el-select
							v-model={scope.searchParam.searchType}
							placeholder="Select"
							style="width: 100px"
							onChange={(value: any) => {
								scope.searchParam.searchType = value;
							}}
						>
							{searchArr.map((item) => (
								<el-option key={item.value} label={item.label} value={item.value} />
							))}
						</el-select>
					),
				};
				return <el-input v-model={scope.modelValue} v-slots={slot} placeholder={getPlaceholder.value} maxlength={50}></el-input>;
			},
		},
	},
	{
		prop: 'productCode',
		label: '物料编码',
		minWidth: 160,
	},
	{
		prop: 'productName',
		label: '物料名称',
		minWidth: 160,
	},
	{
		prop: 'orderType',
		label: '数据类型',
		search: { el: 'select', props: { placeholder: '请选择数据类型', clearable: true, filterable: true } },
		enum: [
			{
				label: '入库',
				value: '入库',
			},
			{
				label: '出库',
				value: '出库',
			},
		],
		minWidth: 100,
		render: (scope: any) => {
			return scope.row.orderType;
		},
	},
	{
		prop: 'createTime',
		label: '操作时间',
		minWidth: 160,
	},
	{
		prop: 'accountsNum',
		label: '出入库数量',
		minWidth: 160,
	},
	{
		prop: 'inventoryName',
		label: '出入库仓库名称',
		minWidth: 160,
	},
];

const getPlaceholder = computed(() => {
	const target = searchArr.find((item) => {
		return item.value == proTable.value?.searchParam.searchType;
	});
	return target ? `输入${target?.label}搜索` : '';
});

// 页面加载时
onMounted(() => {
	NextLoading.done();
});
</script>

<style lang="scss">
.demo-tabs {
	.el-tabs__header {
		margin-bottom: 0;
	}
}
</style>
